﻿#region 导入名称空间

using System;
using System.ComponentModel;
using System.Globalization;
using Autofac.Configuration.Elements;

#endregion

namespace LightWork.Autofac.TypeConversion
{
    #region 配置集合类型 及类型转换器 定义

    public class NameValueCollectionConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(DictionaryElementCollection) || base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value == null)
                return null;
            var castValue = value as DictionaryElementCollection;
            if (castValue == null)
                return base.ConvertFrom(context, culture, value);
            var dict = new NameValueCollection();
            foreach (var item in castValue)
                dict.Add(item.Key, item.Value);
            return dict;
        }
    }

    [TypeConverter(typeof(NameValueCollectionConverter))]
    public class NameValueCollection : Common.Logging.Configuration.NameValueCollection
    {
    }

    #endregion
}